package com.atguigu.yygh.order.service.impl;

import com.atguigu.yygh.enums.PaymentStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.order.mapper.PaymentInfoMapper;
import com.atguigu.yygh.order.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2025/4/1 1:40
 */
@Service
public class PaymentInfoServiceImpl implements PaymentInfoService {
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer status) {
        QueryWrapper<PaymentInfo> paymentInfoQueryWrapper = new QueryWrapper<>();
        paymentInfoQueryWrapper.eq("order_id", orderInfo.getId());
        Integer count = paymentInfoMapper.selectCount(paymentInfoQueryWrapper);// 防止重复插入
        if (count > 0) return;
        // 保存支付信息
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setPaymentType(status);
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        paymentInfoMapper.insert(paymentInfo);
    }

    @Override
    public void UpdatePaymentInfo(Map<String, String> resultMap, String outTradeNo) {
        PaymentInfo paymentInfo = new PaymentInfo();
        UpdateWrapper<PaymentInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("out_trade_no", outTradeNo)
                        .set("payment_status",PaymentStatusEnum.PAID.getStatus());
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));
        paymentInfo.setCallbackContent(resultMap.toString());
        paymentInfo.setCallbackTime(new Date());
        paymentInfoMapper.update(paymentInfo,updateWrapper);
    }

    @Override
    public PaymentInfo findByOrderId(Long orderId) {
        QueryWrapper<PaymentInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", orderId);
        return   paymentInfoMapper.selectOne(queryWrapper);

    }

    @Override
    public void updatePaymentInfoStatusByOrderId(Long orderId, Integer status) {
        PaymentInfo paymentInfo = new PaymentInfo();
        UpdateWrapper<PaymentInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_id", orderId)
                        .set("payment_status", status);
        paymentInfo.setUpdateTime(new Date());
        paymentInfoMapper.update(paymentInfo, updateWrapper);
    }
}
